<style scoped>
</style>

<style>
</style>

<template>
<div class="oss_file">
    <input type="file" id='test_upload' multiple="true" @change="getToken"/>
</div>
</template>

<script>
import productTitle from '../../components/productTitle'
import { getStsTokenWrite,getStsTokenRead } from '../../api/common';
// import co from 'co';
// import OSS from 'ali-oss';
export default {
    data() {
        return {    
            sts:{},
            resultUpload:[]
        }
    },
    components: {
    },
    created() {
        // this.getToken();
        this.gettest()
    },
    mounted() {
    },
    methods: {
        gettest(){
            new Promise((resolve, reject) => {
                getStsTokenRead().then(response => {
                    console.log(response)
                })
            })
            
        },
        getToken() {
            new Promise((resolve, reject) => {
                getStsTokenWrite().then(response => {
                    if (response.data.err == 0) {
                        this.sts = response.data.data.Credentials;
                        var client = new OSS.Wrapper({
                            region:'oss-cn-hangzhou',
                            accessKeyId: this.sts.AccessKeyId,
                            accessKeySecret: this.sts.AccessKeySecret,
                            stsToken: this.sts.SecurityToken,
                            bucket: 'kuaibao-imgs',
                            secure: true,
                        });
                        // client.list({
                        //     'max-keys': 10
                        // }).then(function (result) {
                        //     console.log(result);
                        // }).catch(function (err) {
                        //     console.log(err);
                        // });
                        const files = document.getElementById('test_upload');
                        if(files.files){
                            const file = files.files[0]
                            console.log(file)
                            const storeAs = file.name
                            client.multipartUpload(storeAs, file, {

                            }).then((results) => {
                                console.log(results)
                                if(results.url){
                                    this.resultUpload.push(results.url);
                                }else{
                                    if(results.name === file.name){
                                        const url = this.bucketUrl+ file.name
                                        this.resultUpload.push(url);
                                    }
                                }
                            }).catch((err) => {
                                console.log(err)
                            })
                        }
                    } else {
                        this.$Message.error(response.data.info);
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
    }
}
</script>
